package com.smritimap.demo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Created by zxn on 2024/10/20.
 */
public class RoundedCornerView extends View {

    private Paint paint;
    private float cornerRadius = 100.0f; // 设置圆角的半径

    public RoundedCornerView(Context context) {
        super(context);
        init();
    }

    public RoundedCornerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLUE); // 设置画笔颜色
        //paint.setStyle(Paint.Style.FILL); // 设置填充样式
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        paint.setAntiAlias(true);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 获取View的宽度和高度
        float width = getWidth();
        float height = getHeight();

        // 创建一个路径
        Path path = new Path();
        // 添加四个圆角
        path.moveTo(cornerRadius, 0);
        path.lineTo(width - cornerRadius, 0);

        //float left, float top, float right, float bottom, float startAngle,float sweepAngle
//        path.addArc(20,0,cornerRadius,cornerRadius,0f,180f);

        path.quadTo(width, 0, width, cornerRadius);
        path.lineTo(width, height - cornerRadius);
        path.quadTo(width, height, width - cornerRadius, height);
        path.lineTo(cornerRadius, height);
        path.quadTo(0, height, 0, height - cornerRadius);
        path.lineTo(0, cornerRadius);
        path.quadTo(0, 0, cornerRadius, 0);

        // 关闭路径
        path.close();

        // 使用路径绘制圆角矩形
        canvas.drawPath(path, paint);
    }
}
